// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_FIN_CASH_FLOW
// * CreateDate    : 2012/1/3 10:54:20
// * Creater       : CodeSmith
// * LastChangeDate: 2012/1/3 10:54:20
// * LastChanger   : 2012/1/3 10:54:20
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;
using ESOL.Common;

namespace ESOL.Business.Fin
{
/// <summary>
    /// FinCashFlow
    /// </summary>
    public partial class FinCashFlowBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objFinCashFlowEntity">Entity</param>
        /// <returns>Entity</returns>
        public static FinCashFlowEntity GetFinCashFlow(FinCashFlowEntity objFinCashFlowEntity)
        {
            return DataAccess.SelectSingle<FinCashFlowEntity>(objFinCashFlowEntity);
        }
        public static FinCashFlowEntity GetByID(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return null;
            }
            FinCashFlowEntity entity = new FinCashFlowEntity();
            entity.CashFlowId = id;
            return DataAccess.SelectSingle<FinCashFlowEntity>(entity);
        }
        public static DataTable GetByCondition(string condition, string orderBy, string rowCount)
        {
            string sql = CommonFunctions.GetSelect(rowCount) + @" CF.*, P.NAME_CN, A.ACCOUNTS_NAME
                            FROM FIN_CASH_FLOW CF
                            LEFT JOIN MST_PERSON P ON P.PERSON_ID = CF.PERON_ID
							LEFT JOIN FIN_ACCOUNTS A ON A.ACCOUNTS_CODE = CF.ACCOUNTS_CODE
                            WHERE 1=1 ";
            if (string.IsNullOrEmpty(condition) == false)
            {
                sql += condition;
            }
            if (string.IsNullOrEmpty(orderBy) == false)
            {
                sql += " ORDER BY " + orderBy;
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }
        public static DataTable GetByCondition(string condition)
        {
            string sql = @"SELECT CF.CASH_FLOW_TYPE, CF.CURRENCY, SUM(AMOUNT) AS AMOUNT, A.ACCOUNTS_NAME
                            FROM FIN_CASH_FLOW CF
                            LEFT JOIN MST_PERSON P ON P.PERSON_ID = CF.PERON_ID
							LEFT JOIN FIN_ACCOUNTS A ON A.ACCOUNTS_CODE = CF.ACCOUNTS_CODE
                            WHERE 1=1 ";
            if (string.IsNullOrEmpty(condition) == false)
            {
                sql += condition;
            }
            sql += " GROUP BY CF.CASH_FLOW_TYPE, CF.CURRENCY, A.ACCOUNTS_NAME ";
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }
        public static DataTable GetByCondition(string condition, DateTime date)
        {
            string thisMonth = date.ToString("yyyy-MM");
            string thisYear = date.ToString("yyyy");
            string lastYearMonth = date.AddYears(-1).ToString("yyyy-MM");
            string sql = @"SELECT C.ITEM_NAME, SUBSTRING(C.ITEM_CODE,4,2) AS ITEM_INDEX, TM.AMOUNT AS THIS_MONTH_AMOUNT, Y.AMOUNT AS THIS_YEAR_AMOUNT, LM.AMOUNT AS LAST_YEAR_MONTH_AMOUNT
                            FROM MST_COMMON_ITEM C
                            LEFT JOIN VIEW_CASH_FLOW_TYPE_MONTH TM ON C.ITEM_CODE = TM.CASH_FLOW_TYPE AND TM.CASH_FLOW_DATE = '" + thisMonth + @"'
                            LEFT JOIN VIEW_CASH_FLOW_TYPE_YEAR Y ON C.ITEM_CODE = Y.CASH_FLOW_TYPE AND Y.CASH_FLOW_DATE = '" + thisYear + @"'
                            LEFT JOIN VIEW_CASH_FLOW_TYPE_MONTH LM ON C.ITEM_CODE = LM.CASH_FLOW_TYPE AND LM.CASH_FLOW_DATE = '" + lastYearMonth + @"'
                            WHERE C.ITEM_TYPE = 'CFT' ";
            if (string.IsNullOrEmpty(condition) == false)
            {
                sql += condition;
            }
            sql += " ORDER BY C.ITEM_CODE ";
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }
        #endregion

        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objFinCashFlowEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertFinCashFlow(FinCashFlowEntity objFinCashFlowEntity)
        {
            return DataAccess.Insert(objFinCashFlowEntity);
        }

        public static bool InsertFinCashFlow(List<FinCashFlowEntity> objFinCashFlowEntityList)
        {
            return DataAccess.Insert(objFinCashFlowEntityList);
        }
        #endregion

        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objFinCashFlowEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateFinCashFlow(FinCashFlowEntity objFinCashFlowEntity)
        {
            return DataAccess.Update(objFinCashFlowEntity);
        }

        public static bool UpdateFinCashFlow(List<FinCashFlowEntity> objFinCashFlowEntityList)
        {
            return DataAccess.Update<FinCashFlowEntity>(objFinCashFlowEntityList);
        }
        #endregion

        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objFinCashFlowEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteFinCashFlow(FinCashFlowEntity objFinCashFlowEntity)
        {
            return DataAccess.Delete(objFinCashFlowEntity);
        }

        public static bool DeleteFinCashFlow(List<FinCashFlowEntity> objFinCashFlowEntityList)
        {
            return DataAccess.Delete<FinCashFlowEntity>(objFinCashFlowEntityList);
        }
        #endregion

        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetFinCashFlowAll()
        {
            return DataAccess.SelectAllDataSet<FinCashFlowEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<FinCashFlowEntity> GetFinCashFlowAllList()
        {
            return DataAccess.SelectAll<FinCashFlowEntity>();
        }
        #endregion

        #region Functions
        public static void InsertCashFlow(FinCashFlowEntity cashFlow, DataAccessBroker broker)
        {
            DataAccess.Insert(cashFlow, broker);
            FinCashAmountEntity cashAmount = FinCashAmountBs.GetByCurrency(cashFlow.Currency);
            if (cashAmount == null)
            {
                cashAmount = new FinCashAmountEntity();
                cashAmount.CashAmountId = Guid.NewGuid().ToString();
                cashAmount.TotalAmount = cashFlow.Amount;
                cashAmount.Currency = cashFlow.Currency;
                DataAccess.Insert(cashAmount, broker);
            }
            else
            {
                cashAmount.TotalAmount += cashFlow.Amount;
                DataAccess.Update(cashAmount, broker);
            }
        }
        public static bool SaveCashFlow(FinCashFlowEntity cashFlow, FinCashFlowEntity oldCashFlow)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (oldCashFlow == null)
                    {
                        //Insert
                        DataAccess.Insert(cashFlow, broker);
                        FinCashAmountEntity cashAmount = FinCashAmountBs.GetByCurrency(cashFlow.Currency);
                        if (cashAmount == null)
                        {
                            cashAmount = new FinCashAmountEntity();
                            cashAmount.CashAmountId = Guid.NewGuid().ToString();
                            cashAmount.TotalAmount = cashFlow.Amount;
                            cashAmount.Currency = cashFlow.Currency;
                            DataAccess.Insert(cashAmount, broker);
                        }
                        else
                        {
                            cashAmount.TotalAmount += cashFlow.Amount;
                            DataAccess.Update(cashAmount, broker);
                        }
                    }
                    else
                    {
                        //Update
                        DataAccess.Update(cashFlow, broker);
                        if (cashFlow.Currency != oldCashFlow.Currency)
                        {
                            FinCashAmountEntity cashAmount = FinCashAmountBs.GetByCurrency(cashFlow.Currency);
                            FinCashAmountEntity oldCashAmount = FinCashAmountBs.GetByCurrency(oldCashFlow.Currency);
                            if (oldCashAmount != null)
                            {
                                oldCashAmount.TotalAmount = oldCashAmount.TotalAmount - oldCashFlow.Amount;
                                DataAccess.Update(oldCashAmount, broker);
                            }
                            if (cashAmount == null)
                            {
                                cashAmount = new FinCashAmountEntity();
                                cashAmount.CashAmountId = Guid.NewGuid().ToString();
                                cashAmount.TotalAmount = cashFlow.Amount;
                                cashAmount.Currency = cashFlow.Currency;
                                DataAccess.Insert(cashAmount, broker);
                            }
                            else
                            {
                                cashAmount.TotalAmount += cashFlow.Amount;
                                DataAccess.Update(cashAmount, broker);
                            }
                        }
                        else if (cashFlow.Amount != oldCashFlow.Amount)
                        {
                            FinCashAmountEntity cashAmount = FinCashAmountBs.GetByCurrency(cashFlow.Currency);
                            if (cashAmount != null)
                            {
                                cashAmount.TotalAmount = cashAmount.TotalAmount + cashFlow.Amount - oldCashFlow.Amount;
                                DataAccess.Update(cashAmount, broker);
                            }
                        }
                    }

                    broker.Commit();
                    return true;
                }
                catch(Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }
        public static bool DeleteCashFlow(FinCashFlowEntity cashFlow)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    //Update
                    FinCashAmountEntity cashAmount = FinCashAmountBs.GetByCurrency(cashFlow.Currency);
                    if (cashAmount != null)
                    {
                        cashAmount.TotalAmount = cashAmount.TotalAmount - cashFlow.Amount;
                        DataAccess.Update(cashAmount, broker);
                    }
                    DataAccess.Delete(cashFlow, broker);

                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }
        #endregion
    }
	
	
}	

